package com.best.controller;

import com.best.pojo.AppUser;
import com.best.pojo.Food;
import com.best.pojo.UserLoginRequest;
import com.best.result.Response;
import com.best.result.ResultCodeEnum;
import com.best.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户
 *
 * @author yangruyi
 * @date 2021/3/12
 */
@RestController
@RequestMapping("/user/")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private UserService userService;
    /**
     * 注册
     *
     * @param user AppUser
     * @return Response
     */
    @PostMapping("registry")
    public Response registry(@RequestBody @Validated AppUser user){
        boolean repeated = userService.isRepeated(user.getUsername());
        if(repeated){
            return Response.setResult(ResultCodeEnum.USERNAME_REPEATED);
        }
        boolean b = userService.addUser(user);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }
    /**
     * 修改信息
     *
     * @param user AppUser
     * @return Response
     */
    @PutMapping("edit")
    public Response edit(@RequestBody AppUser user){
        boolean b = userService.updateUser(user);
        if(!b){
            return Response.error();
        }
        return Response.ok();
    }

    /**
     * 用户个人信息
     *
     * @param id 用户id
     * @return AppUser
     */
    @GetMapping("{id}")
    public AppUser detail(@PathVariable String id){
        return userService.detail(id);
    }

    /**
     * 收藏集
     *
     * @param id 用户id
     * @return List
     */
    @GetMapping("love/{id}")
    public List<Food> loveFood(@PathVariable String id){
        return userService.lovesOfUser(id);
    }

    /**
     * 登录
     *
     * @param request request
     * @return AppUser
     */
    @PostMapping("/login")
    public Response login(@RequestBody @Validated UserLoginRequest request){
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new UsernamePasswordToken(request.getUsername(),request.getPassword()));
            return Response.ok().data("user",userService.authentication(request.getUsername()));
        } catch (UnknownAccountException e) {
            logger.info("msg:{}","用户名错误");
            return Response.setResult(ResultCodeEnum.USERNAME_ERROR);
        }catch (IncorrectCredentialsException e){
            logger.info("msg:{}","密码错误");
            return Response.setResult(ResultCodeEnum.PASSWORD_ERROR);
        }
    }
}
